package com.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: mayu
 * @date: 2021年04月09日 11:36
 */
public class TestWithoutVolatile {
    List lists = new ArrayList<>();

    void add(Object o) {
        lists.add(o);
    }

    int size() {
        return lists.size();
    }

    public static void main(String[] args) {
        TestWithoutVolatile twv = new TestWithoutVolatile();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                twv.add(i);
                System.out.println(i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                if (twv.size() == 5) {
                    break;
                }
            }
            System.out.println("t2结束");
        }).start();
    }
}
